
package com.work.mvc.view.form;

import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.fieldgroup.*;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.validator.BeanValidator;
import com.vaadin.ui.*;
import com.work.constants.view.ComponentsSize;
import com.work.db.entity.*;
import com.work.util.StringConverter;

public class LeaveCustomField extends CustomField<LeaveOfAbsence> {

	private static final long serialVersionUID = -7073831502720290536L;

	private GridLayout gridLayout = new GridLayout(4, 1);

	private FieldGroup fieldGroup = new BeanFieldGroup<LeaveOfAbsence>(LeaveOfAbsence.class);

	private WorkDay workDay;

	public LeaveCustomField(WorkDay workDay) {
		this.workDay = workDay;
		this.gridLayout.setSpacing(true);
		this.gridLayout.setSizeFull();
		this.setSizeFull();
	}

	@Override
	protected Component initContent()
	{
		initComponents();
		setDataSource(workDay.getLeaveOfAbsence());
		bindFieldsToProperties();
		buildForm();

		return gridLayout;
	}

	//pole "Rodzaj urlopu"
	private ComboBox leaveTypeField;

	//pole "Komentarz"
	private TextField commentField;

	private void initComponents()
	{
		leaveTypeField = new ComboBox();
		leaveTypeField.setCaption(LeaveFieldsEnum.LEAVE_TYPE.getFieldLabel());
		leaveTypeField.setWidth(ComponentsSize.FULL);
		leaveTypeField.setImmediate(true);
		leaveTypeField.addValidator(new BeanValidator(LeaveOfAbsence.class, LeaveFieldsEnum.LEAVE_TYPE.getFieldName()));
		for (LeaveTypeEnum leaveType : LeaveTypeEnum.values()) {
			leaveTypeField.addItem(leaveType);
		}
		if (workDay.getLeaveOfAbsence() != null) {
			leaveTypeField.setValue(workDay.getLeaveOfAbsence().getLeaveType());
		}

		commentField = new TextField();
		commentField.setCaption(LeaveFieldsEnum.COMMENT.getFieldLabel());
		commentField.setWidth(ComponentsSize.FULL);
		commentField.setImmediate(true);
		commentField.setConverter(new StringConverter());
		commentField.addValidator(new BeanValidator(LeaveOfAbsence.class, LeaveFieldsEnum.COMMENT.getFieldName()));
	}

	private void bindFieldsToProperties()
	{
		fieldGroup.setBuffered(true);
		fieldGroup.bind(leaveTypeField, LeaveFieldsEnum.LEAVE_TYPE.getFieldName());
		fieldGroup.bind(commentField, LeaveFieldsEnum.COMMENT.getFieldName());
	}

	private void buildForm()
	{
		gridLayout.addComponent(leaveTypeField, 0, 0, 1, 0);
		gridLayout.addComponent(commentField, 2, 0, 3, 0);
	}

	public void setDataSource(LeaveOfAbsence leave)
	{
		this.setInternalValue(leave);
		if (leave != null) {
			fieldGroup.setItemDataSource(new BeanItem<LeaveOfAbsence>(leave));
			setValidationVisible(true);
		} else {
			fieldGroup.setItemDataSource(new BeanItem<LeaveOfAbsence>(new LeaveOfAbsence()));
			setValidationVisible(false);
		}
	}

	@Override
	public Class<LeaveOfAbsence> getType()
	{
		return LeaveOfAbsence.class;
	}

	@Override
	public void commit() throws SourceException, InvalidValueException
	{
		try {
			if (this.isValid()) {
				fieldGroup.commit();
			}
		} catch (CommitException e) {
		}
	}

	@Override
	public void validate() throws InvalidValueException
	{
		if (this.getInternalValue() != null) {
			for (Field<?> field : fieldGroup.getFields()) {
				field.validate();
			}
		}
	}

	@Override
	public void setValidationVisible(boolean validateAutomatically)
	{
		super.setValidationVisible(false);
		leaveTypeField.setValidationVisible(validateAutomatically);
		commentField.setValidationVisible(validateAutomatically);
	}
}
